import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import {ElMessage} from "element-plus";
import {ADMIN_ROLE_NAME_EN} from "@/settings/sysMapping";
import {getUserInfo} from "@/manager/UserManager";
// import None from "@/views/None";

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: "/shane/home"
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/hello',
    name: 'Hello',
    component: () => import( '../views/forText/Hello.vue')
  },
  {
    path: '/hello/yy',
    name: 'Hello2',
    component: () => import( '../views/forText/Hello.vue')
  },
  // {
  //   path: '/user/login',
  //   name: 'Login',
  //   component: () => import( '../views/user/Login.vue')
  // },
  {
    path: '/user',
    component: () => import( '../views/None'),
    name: "User",
    children : [
      {
        path: 'login',
        name: 'Login',
        component: () => import('../views/user/Login')
      },
      {
        path: 'register',
        name: 'Register',
        component: () => import('../views/user/Register')
      },
      {
        path: 'user_info',
        name: 'UserInfo',
        component: () => import('../views/user/UserInfo')
      }
    ]
  },{
    path: '/question',
    component: () => import( '../views/None'),
    name: "Question",
    children : [
      {
        path: 'add',
        name: 'AddQuestion',
        component: () => import('../views/Question/Add')
      },
      {
        path: 'info',
        name: 'QuestionInfo',
        component: () => import('../views/Question/QuestionInfo')
      },
      {
        path: 'update',
        name: 'UpdateQuestion',
        component: () => import('../views/Question/Update')
      },
    ]
  },{
    path: '/answer',
    component: () => import( '../views/None'),
    name: "Answer",
    children : [
      {
        path: 'add',
        name: 'AddAnswer',
        component: () => import('../views/Answer/Add')
      },
      {
        path: 'info',
        name: 'AnswerInfo',
        component: () => import('../views/Answer/AnswerInfo')
      },
      {
        path: 'update',
        name: 'UpdateAnswer',
        component: () => import('../views/Answer/Update')
      },
    ]
  },{
    path: '/essay',
    component: () => import( '../views/None'),
    name: "Essay",
    children : [
      {
        path: 'add',
        name: 'AddEssay',
        component: () => import('../views/Essay/Add')
      },
      {
        path: 'info',
        name: 'EssayInfo',
        component: () => import('../views/Essay/EssayInfo')
      },
      {
        path: 'update',
        name: 'UpdateEssay',
        component: () => import('../views/Essay/Update')
      },
    ]
  },{
    path: '/shane',
    component: () => import( '../views/None'),
    name: "Shane",
    children : [
      {
        path: 'home',
        name: 'ShaneHome',
        component: () => import('../views/Shane/ShaneHome')
      },
    ]
  },{
    path: '/Admin',
    component: () => import( '../views/None'),
    name: "Admin",
    children : [
      {
        path: 'question',
        name: 'ManageQuestion',
        component: () => import('../views/Admin/ManageQuestion')
      },
      {
        path: 'answer',
        name: 'ManageAnswer',
        component: () => import('../views/Admin/ManageAnswer')
      },
      {
        path: 'essay',
        name: 'ManageEssay',
        component: () => import('../views/Admin/ManageEssay')
      },
      {
        path: 'user',
        name: 'ManageUser',
        component: () => import('../views/Admin/ManageUser')
      },
    ]
  },{
    path: '/test',
    component: () => import( '../views/None'),
    name: "Test",
    children : [
      {
        path: 'test1',
        name: 'Test1',
        component: () => import('../views/Test/Test1')
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  base: "front",
  routes
})
// router.beforeEach();
router.beforeEach(async (to, from, next) => {
  // console.log(to.query);
  // console.log(from);
  // console.log(next);
  // console.log(to);
  /* 判断用户权限，是否有权限访问当前页面 */
  if (to.path.startsWith("/admin/")) {
    let userInfo = await getUserInfo();
    if (!userInfo.roleNameEnList.includes(ADMIN_ROLE_NAME_EN)) {
      ElMessage({
        showClose: true,
        message: "您没有权限访问",
        type: 'success',
        center: true,
      });
      next({path: from.path})
    }
  }

  next();
});
export default router
